package com.demo.controller;

import com.demo.dto.UserDto;
import com.demo.pojo.Comployee;
import com.demo.pojo.User;
import com.demo.service.ComployeeService;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author： Administrator
 * @date: 21/05/11 18:46
 * 描述：
 */
@Controller
public class InfoController {
    @Autowired
    private UserService userService;
    @Autowired
    private ComployeeService comployeeService;

    @PostMapping("/login.do")
    @ResponseBody
    public Map<String, Object> login(User user, HttpSession session, HttpServletResponse response) {
        Map<String, Object> map = userService.login(user);
        int code = (int) map.get("code");
        if (code == 0) {
            user = (User) map.get("data");
            user.setPwd(null);
            session.setAttribute("user", user);
            Cookie cookie = new Cookie("username", user.getUsername());
            cookie.setPath("/fixedAsset");
            cookie.setMaxAge(60 * 60 * 24 * 10);
            response.addCookie(cookie);
        }
        map.remove("data");
        return map;
    }

    @RequestMapping("/getUser.do")
    @ResponseBody
    public Map<String, Object> getUser(String comployee, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            map.put("code", 1);
            map.put("message", "未登录");
            return map;
        }
        if (comployee == null) {
            map.put("code", 0);
            map.put("data", user);
            map.put("message", "用户的信息");
            return map;
        }
        UserDto userDto = userService.selectDto(user.getUsername());
        map.put("code", 0);
        map.put("data", userDto);
        map.put("message", "用户的全部信息");
        return map;
    }

    @PostMapping("/update.do")
    @ResponseBody
    public Map<String, Object> update(Comployee comployee, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        comployee.setComployeeNo(user.getComployeeNo());
        int update = comployeeService.update(comployee);
        if (update > 0) {
            map.put("code", 0);
            map.put("message", "修改成功！");
            return map;
        }
        map.put("code", 1);
        map.put("message", "修改失败！");
        return map;
    }

    @PostMapping("/updatePassword.do")
    @ResponseBody
    public Map<String, Object> updatePassword(String password, String newPassword, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Map<String, Object> map = userService.updatePassword(user.getUsername(), password, newPassword);
        int code = (int) map.get("code");
        if (code == 0) {
            session.removeAttribute("user");
        }
        return map;
    }

    @RequestMapping("/outLogin.do")
    @ResponseBody
    public Map<String, Object> outLogin(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("message", "退出登录成功！");
        session.removeAttribute("user");
        return map;
    }

}
